package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.dao.BankerDao;
import com.ytjj.qmyx.admin.dao.BankerMessageDao;
import com.ytjj.qmyx.admin.dao.BankerProductDao;
import com.ytjj.qmyx.admin.dao.EvaluationProductDao;
import com.ytjj.qmyx.admin.model.request.EvaluationProductMatchingRequest;
import com.ytjj.qmyx.admin.model.request.EvaluationProductRequeset;
import com.ytjj.qmyx.admin.model.request.EvaluationRequest;
import com.ytjj.qmyx.admin.model.response.EvaluationProductResponse;
import com.ytjj.qmyx.admin.service.EvaluationProductService;
import com.ytjj.qmyx.mall.mapper.*;
import com.ytjj.qmyx.mall.model.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 *
 *
 * @Version: 1.0
 */
@Service
public class EvaluationProductServiceImpl implements EvaluationProductService {

    @Autowired
    private EvaluationProductDao evaluationProductDao;

    @Autowired
    private BankerProductDao bankerProductDao;

    @Autowired
    private BankerProductImgMapper bankerProductImgMapper;

    @Autowired
    private BankerProductSpecMapper bankerProductSpecMapper;

    @Autowired
    private BankerProductSkuKeyMapper bankerProductSkuKeyMapper;

    @Autowired
    private BankerProductSkuValueMapper bankerProductSkuValueMapper;

    @Autowired
    private EvaluationProductImgMapper evaluationProductImgMapper;

    @Autowired
    private EvaluationProductSpecMapper evaluationProductSpecMapper;

    @Autowired
    private EvaluationProductSkuKeyMapper evaluationProductSkuKeyMapper;

    @Autowired
    private EvaluationProductSkuValueMapper evaluationProductSkuValueMapper;

    @Autowired
    private BankerDao bankerDao;

    @Autowired
    private BankerMessageDao bankerMessageDao;

    @Resource
    private EvaluationProductMatchingMapper evaluationProductMatchingMapper;

    @Resource
    private EvaluationProductMapper evaluationProductMapper;

    @Resource
    private BankerSpecialMapper bankerSpecialMapper;


    @Override
    public CommonPage<EvaluationProductResponse> selectList(EvaluationRequest evaluationRequest) {
        PageHelper.startPage(evaluationRequest.getPageNum(), evaluationRequest.getPageSize());
        List<EvaluationProductResponse> pageList = evaluationProductDao.selectList(evaluationRequest);
        if(!CollectionUtils.isEmpty(pageList)) {
            for(EvaluationProductResponse ep : pageList) {
                Integer activityType = evaluationProductDao.getActivityType(ep.getBankerProductId());
                ep.setActivityType(activityType);
            }
        }
        return CommonPage.restPage(pageList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertEvaluation(EvaluationProduct evaluationProduct) {

        //审核时判断该供应商商品是否审核通过
        Integer isExists = evaluationProductDao.isExists(evaluationProduct.getId());
        if (isExists > 0) {
            throw new ApiException("该供应商商品已审核通过，暂不支持重复审核！");
        }

        // 供应商商品-->测评商品 审核通过 1.审核状态--通过 状态--上架 2.商品信息同步
        evaluationProduct.setAuditStatus(1);
        evaluationProduct.setStatus((byte)1);
        evaluationProduct.setCreateTime(new Date());
        Integer count = evaluationProductDao.insertEvaluation(evaluationProduct);

        // Mybatis主键自增长策略 返回自增测评商品主键id
        Integer evaluation = evaluationProduct.getId();

        if (count > 0) {
            // 1.同步图片img
            BankerProductImgExample bankerProductImgExample = new BankerProductImgExample();
            bankerProductImgExample.createCriteria().andProductIdEqualTo(evaluationProduct.getBankerProductId());
            List<BankerProductImg> bankerImgList = bankerProductImgMapper.selectByExample(bankerProductImgExample);
            if (!CollectionUtils.isEmpty(bankerImgList)) {
                for(BankerProductImg bpImg : bankerImgList) {
                    EvaluationProductImg epi = new EvaluationProductImg();
                    epi.setBankerId(bpImg.getBankerId());
                    epi.setProductId(evaluation);
                    epi.setImgUrl(bpImg.getImgUrl());
                    epi.setSort(bpImg.getSort());
                    epi.setFileType(bpImg.getFileType());
                    epi.setType(bpImg.getType());
                    epi.setCreateTime(new Date());
                    evaluationProductImgMapper.insertSelective(epi);
                }
            }

            // 2.同步规格spec
            BankerProductSpecExample bankerProductSpecExample = new BankerProductSpecExample();
            bankerProductSpecExample.createCriteria().andProductIdEqualTo(evaluationProduct.getBankerProductId());
            List<BankerProductSpec> bankerSpecList = bankerProductSpecMapper.selectByExample(bankerProductSpecExample);
            if (!CollectionUtils.isEmpty(bankerSpecList)) {
                for (BankerProductSpec bpSpec : bankerSpecList) {
                    EvaluationProductSpec eps = new EvaluationProductSpec();
                    eps.setBankerId(bpSpec.getBankerId());
                    eps.setProductId(evaluation);
                    eps.setSkuSpec(bpSpec.getSkuSpec());
                    eps.setPrice(evaluationProduct.getPrice());
                    eps.setProImg(bpSpec.getProImg());
                    eps.setStock(bpSpec.getStock());
                    eps.setStatus((byte)1);
                    eps.setCreateTime(new Date());
                    evaluationProductSpecMapper.insertSelective(eps);
                }
            }

            // 3.同步sku_key
            BankerProductSkuKeyExample bankerProductSkuKeyExample = new BankerProductSkuKeyExample();
            bankerProductSkuKeyExample.createCriteria().andPidEqualTo(evaluationProduct.getBankerProductId());
            List<BankerProductSkuKey> bankerKeyList = bankerProductSkuKeyMapper.selectByExample(bankerProductSkuKeyExample);
            if (!CollectionUtils.isEmpty(bankerKeyList)) {
                for (BankerProductSkuKey bpsk : bankerKeyList) {
                    EvaluationProductSkuKey epsk = new EvaluationProductSkuKey();
                    epsk.setPid(evaluation);
                    epsk.setName(bpsk.getName());
                    epsk.setSort(bpsk.getSort());
                    epsk.setCreateTime(new Date());
                    epsk.setKid(bpsk.getKid());
                    evaluationProductSkuKeyMapper.insertSelective(epsk);
                }
            }

            // 4.同步sku_value
            BankerProductSkuValueExample bankerProductSkuValueExample = new BankerProductSkuValueExample();
            bankerProductSkuValueExample.createCriteria().andPidEqualTo(evaluationProduct.getBankerProductId());
            List<BankerProductSkuValue> bpsvList = bankerProductSkuValueMapper.selectByExample(bankerProductSkuValueExample);
            if (!CollectionUtils.isEmpty(bpsvList)) {
                for (BankerProductSkuValue bpsv : bpsvList) {
                    EvaluationProductSkuValue epsv = new EvaluationProductSkuValue();
                    epsv.setPid(evaluation);
                    epsv.setVid(bpsv.getVid());
                    epsv.setValue(bpsv.getValue());
                    epsv.setSort(bpsv.getSort());
                    epsv.setCreateTime(new Date());
                    epsv.setStatus(bpsv.getStatus());
                    evaluationProductSkuValueMapper.insertSelective(epsv);
                }
            }

            // 5.供应商商品状态同步
            bankerProductDao.updateAuditStatus(evaluationProduct.getBankerProductId(), 1,evaluationProduct.getAppraisalType());

//            // 6.供应商 商品总数+1
//            bankerDao.addProductNum(evaluationProduct.getBankerId());

//            // 7.同步供应商 0元适用商品数量
//            Integer zeroNum = evaluationProductDao.getZeroCount(evaluationProduct.getBankerId());
//            bankerDao.syncZeroNum(evaluationProduct.getBankerId(), zeroNum);

            // 8.供应商消息通知
            BankerMessage message = new BankerMessage();
            message.setBankerId(evaluationProduct.getBankerId());
            message.setBankerProductId(evaluationProduct.getBankerProductId());
            message.setProductName(evaluationProduct.getProductName());
            message.setRemarks("您上传的商品,名称："+evaluationProduct.getProductName()+",已通过审核，为正常上架状态！");
            message.setStatus(0);
            message.setType(1);
            message.setCreateTime(new Date());
            bankerMessageDao.insertMessage(message);

        }

        return count;
    }

    @Override
    public Integer updateStatus(Integer id) {
        EvaluationProduct evaluation = evaluationProductDao.getEvaluation(id);
        // 商品上下架状态 同步供应商产品上下架状态 同步供应商产品数banker-productNum的增加减少操作
        Integer count = 0;
        if(evaluation.getStatus() == 1) {
            count = evaluationProductDao.updateStatus(id, 2);
            if(count > 0) {
//                bankerProductDao.updateStatus(evaluation.getBankerProductId(), 2);
//                bankerDao.subProductNum(evaluation.getBankerId());

                // 供应商消息通知--商品下架通知
                BankerMessage message = new BankerMessage();
                message.setBankerId(evaluation.getBankerId());
                message.setBankerProductId(evaluation.getBankerProductId());
                message.setProductName(evaluation.getProductName());
                message.setRemarks("您上传的商品,名称："+evaluation.getProductName()+",现为上架状态！");
                message.setStatus(0);
                message.setType(1);
                message.setCreateTime(new Date());
                bankerMessageDao.insertMessage(message);
            }
        } else if(evaluation.getStatus() == 2) {
            count = evaluationProductDao.updateStatus(id, 1);
            if(count > 0) {
//                bankerProductDao.updateStatus(evaluation.getBankerProductId(), 1);
//                bankerDao.addProductNum(evaluation.getBankerId());

                // 供应商消息通知--商品下架通知
                BankerMessage message = new BankerMessage();
                message.setBankerId(evaluation.getBankerId());
                message.setBankerProductId(evaluation.getBankerProductId());
                message.setProductName(evaluation.getProductName());
                message.setRemarks("您上传的商品,名称："+evaluation.getProductName()+",现为下架状态！");
                message.setStatus(0);
                message.setType(1);
                message.setCreateTime(new Date());
                bankerMessageDao.insertMessage(message);
            }
        } else {
//            if(count > 0) {
//                bankerProductDao.updateStatus(evaluation.getBankerProductId(), 3);
//                bankerDao.subProductNum(evaluation.getBankerId());
//            }
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateEvaluation(EvaluationProductRequeset evaluationProductRequeset) {
        // 查询之前的活动类型
        EvaluationProduct ep = evaluationProductMapper.selectByPrimaryKey(evaluationProductRequeset.getId());
        Integer bankerProductId = ep.getBankerProductId();
        evaluationProductDao.updateBankerSpecial(bankerProductId, evaluationProductRequeset.getActivityType());
        return evaluationProductDao.updateEvaluation(evaluationProductRequeset);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editEvaluationProductMatching(EvaluationProductMatchingRequest evaluationProductMatchingRequest) {
        // 查询库里是否有该条数据
        Integer productId = evaluationProductMatchingRequest.getProductId();
        EvaluationProductMatchingExample evaluationProductMatchingExample = new EvaluationProductMatchingExample();
        evaluationProductMatchingExample.createCriteria()
                .andProductIdEqualTo(productId);
        List<EvaluationProductMatching> evaluationProductMatchings = evaluationProductMatchingMapper.selectByExampleWithBLOBs(evaluationProductMatchingExample);
        if(evaluationProductMatchings.size() > 0) {
            EvaluationProductMatching evaluationProductMatching = evaluationProductMatchings.get(0);
            evaluationProductMatchingRequest.setId(evaluationProductMatching.getId());
            evaluationProductMatching.setUpdateTime(new Date());
            evaluationProductMatchingRequest.setCreateTime(evaluationProductMatching.getCreateTime());
            BeanUtils.copyProperties(evaluationProductMatchingRequest, evaluationProductMatching);
            evaluationProductMatchingMapper.updateByPrimaryKeyWithBLOBs(evaluationProductMatching);
            return;
        }
        // 无插入
        EvaluationProductMatching evaluationProductMatching = new EvaluationProductMatching();
        evaluationProductMatching.setProductId(evaluationProductMatchingRequest.getProductId());
        evaluationProductMatching.setAgeMax(evaluationProductMatchingRequest.getAgeMax());
        evaluationProductMatching.setAgeMin(evaluationProductMatchingRequest.getAgeMin());
        evaluationProductMatching.setAgeStr(evaluationProductMatchingRequest.getAgeStr());
        evaluationProductMatching.setComsumerPower(evaluationProductMatchingRequest.getComsumerPower());
        evaluationProductMatching.setLabelJson(evaluationProductMatchingRequest.getLabelJson());
        evaluationProductMatching.setProvinceJson(evaluationProductMatchingRequest.getProvinceJson());
        evaluationProductMatching.setCreateTime(new Date());
        evaluationProductMatching.setSex(evaluationProductMatchingRequest.getSex());
        evaluationProductMatchingMapper.insertSelective(evaluationProductMatching);
    }

    @Override
    public EvaluationProductMatching getEvaluationProductMatching(Integer productId) {
        EvaluationProductMatchingExample evaluationProductMatchingExample = new EvaluationProductMatchingExample();
        evaluationProductMatchingExample.createCriteria()
                .andProductIdEqualTo(productId);
        List<EvaluationProductMatching> evaluationProductMatchings = evaluationProductMatchingMapper.selectByExampleWithBLOBs(evaluationProductMatchingExample);
        if(evaluationProductMatchings != null && evaluationProductMatchings.size() > 0) {
            return evaluationProductMatchings.get(0);
        }
        return null;
    }

    @Override
    public void updateEvaluationActivityType(Integer id) {


    }
}
